var pnum = "";   
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findMentionsPageListByCondition",
	        data: param,
	        async: true,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"mention_information_code": "资料代号"
                                ,"mention_information_name": "资料名称"
                                ,"mention_information_type":"是否结构化数据"
                                ,"order_num":"排序规则"
                                ,"create_time":"创建时间"
                                ,"update_time":"修改时间"
                                ,"project_design_major_name":"所属专业"
                                ,"operation":"操作"
	            			},
                            "stateData":[{"stateField":"mention_information_type","stateArray":[{1:"是"},{0:"否"}]}],
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"delObj","name":"删除"},
							       {"event":"updateFormField","name":"编辑表单属性"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
   }

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           mention_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findMentionsPageListByCondition",
           data: param,
           async: true,
           success: function (data, textStatus, jqXHR) {
               var pageCount = data.pager.pageCount;
               var dataObj ={
                   "list":data.list,
                   "dataTool":{
                       "title":{
                           "mention_information_code": "资料代号"
                           ,"mention_information_name": "资料名称"
                           ,"mention_information_type":"是否结构化数据"
                           ,"order_num":"排序规则"
                           ,"create_time":"创建时间"
                           ,"update_time":"修改时间"
                           ,"project_design_major_name":"所属专业"
                           ,"operation":"操作"
                       },
                       "titleCheckbox":false,
                       "btn":[{"event":"updateObj","name":"编辑"},
                           {"event":"delObj","name":"删除"},
                           {"event":"updateFormField","name":"编辑表单属性"}]
                   }
               };
               showPageTable('view', dataObj,num,pageCount,"searchObj");
           },
           error: function () {
           }
       });
   }

   //编辑提入资料表单信息
   var updateFormField = function(obj){
	   var type = obj.mention_information_type
	   var code = "dc_mention_"+obj.mention_information_code;
	   if(type=="1" || type == "true"){
		   openStructurLay(code);
	   }else{
		   //非结构化数据
		   openNoStructurLay(code);
	   }
   }
   
   //打开结构化数据维护弹窗
   var openStructurLay = function(code){
		layer.open({
		  	title:'编辑结构化表单'
			,content:initFormField(code)
	    	,area: ['1150px', '800px']
			,scrollbar: false
		  	,btn: ['保存', '取消']
			,yes: function(index,layero){ //确认
				commit_field_data(code);
			}
	    });
   }
   
   //打开非结构化数据维护弹窗
   var openNoStructurLay = function(code){
		layer.open({
		  	title:'编辑非结构化表单'
			,content:initNoFormField(code)
	    	,area: ['1150px', '800px']
			,scrollbar: false
		  	,btn: ['保存', '取消']
			,yes: function(index,layero){ //确认
				commit_field_data(code);
			}
	    });
   }
   
   
   //校验数据
   var verify_data = function(){
	   var juage = true;
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index>1){
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var obj_name = $(this).children().attr("name");
					   var obj_val = $(this).children().val();
					   if("field_type"==obj_name && ("varchar"==obj_val || "int"==obj_val || "double" == obj_val)){
						   var field_length_obj = $(this).parent().find("td input[name='field_length']");
						   if(field_length_obj.val() == "" || field_length_obj.val() == undefined){
							   field_length_obj.focus();
							   layer.tips('字段长度不能为空', field_length_obj);
							   juage = false;
							   return false;
						   }
					   }
					   if("field_name"==obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='field_name']");
						   field_name_obj.focus();
						   layer.tips('字段名不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }
					   if("widget_length"==obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='widget_length']");
						   field_name_obj.focus();
						   layer.tips('控件长度不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }
					   if("field_comment"==obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='field_comment']");
						   field_name_obj.focus();
						   layer.tips('字段备注不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }
					/*   if("field_order" == obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='field_order']");
						   field_name_obj.focus();
						   layer.tips('字段排序不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }*/
				   }
			   });
			   if(!juage){
				   return false;
			   }
		   }
	   });
	   return juage;
   }
   
   //封装结构化表单数据
   var commit_field_data = function(code){
	   if(false==verify_data()){
		   return false;
	   }
	   var json_array = [];
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index > 1){
			   var json_str = {};
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var input_obj = $(this).children();
					   var input_name = $(this).children().attr("name");
					   if(input_name=="field_name"){
						   json_str['field_name']=$.trim(input_obj.val());
					   }else if(input_name=="field_comment"){
						   json_str['field_comment']=$.trim(input_obj.val());
					   }else if(input_name=="field_length"){
						   json_str['field_length']=$.trim(input_obj.val());
					   }else if(input_name=="field_default"){
						   json_str['field_default']=$.trim(input_obj.val());
					   }else if(input_name=="field_type"){
						   json_str['field_type']=$.trim(input_obj.val());
					   }else if(input_name=="is_null"){
						   if(input_obj.is(':checked')){
							   json_str['is_null']='true';
						   }else{
							   json_str['is_null']='false';
						   }
					   }else if(input_name=="widget_type"){
						   json_str['widget_type']=$.trim(input_obj.val());
					   }else if(input_name=="widget_length"){
						   json_str['widget_length']=$.trim(input_obj.val());
					   }else if(input_name=="is_show"){
						   if(input_obj.is(':checked')){
							   json_str['is_show']='true';
						   }else{
							   json_str['is_show']='false';
						   }
					   }else if(input_name=="form_type"){
						   json_str['form_type']=$.trim(input_obj.val());
					   }else if(input_name=="form_default"){
						   json_str['form_default']=$.trim(input_obj.val());
					   }else if(input_name=="field_order"){
						   json_str['field_order']=$.trim(input_obj.val());
					   }
				   }
			   });
			   json_array.push(json_str);
		   }
	   });
	   var field_list = JSON.stringify(json_array);
	   var table_name = code;
	   var param = {"table_name":table_name,"data_list":field_list};
	   commit_data(param);
   } 
   
   var commit_data = function(param){
	   $.post(base+"/dataCenter/businessdata/updateTableField",param,
	       function(data,status){
		     if(data.statusCode == 0){
			  	layer.msg(data.message, {
					icon : 1
			  	});
		     }else{
				layer.msg(data.message, {
					icon : 5
				});
		    }
	  });
   }
   
   //取得表单信息
   var initFormField = function(code){
	   var contentDiv;
	   $.ajax({
		   url:base+"/dataCenter/findFromFieldHtml/"+code,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
   }
   
   //取得非结构表单信息
   var initNoFormField = function(code){
	   var contentDiv;
	   $.ajax({
		   url:base+"/dataCenter/findNoFromFieldHtml/"+code,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
   }
   
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id,code:obj.mention_information_code}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delMentionById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   var addObj = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "mention_code":"资料代码",
					   "mention_name":"资料名称",
					   "major_id":"专业名称",
					   "mention_type":"是否结构化数据",
					   "order_num":"排序规则"
				   },"text":{
					   "mention_code":"",
					   "mention_name":"",
					   "major_id":"",
					   "mention_type":"",
					   "order_num":""
				   },"input":{
					   "mention_code":"text",
					   "mention_name":"text",
					   "major_id":"select",
					   "mention_type":"switch",
					   "order_num":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initDesignMajor();
	   initSwitch();
   }
   
   var initDesignMajor = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
   		  	function(data,status){
     		    	if(data!=''){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
   	                });
   	                $("#select").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("#select").html('<option value=""></option>');
   	      		}
     		    form.render(); 
  });
  }
   
   var initSwitch = function(flag){
	   var html = '';
	   if(flag=="true" || flag=="1"){
		   html += '<input checked="" name="mention_type" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
	   }else{
		   html += '<input name="mention_type" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
	   }
	   $("#switch").html(html);
	   form.render(); 
   }
   
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"唯一标识",
						    "major_id":"专业id",
							"mention_type":"是否结构化数据",
							"order_num":"排序规则",
					      	"major_name":"所属专业",
					      	"mention_code": "资料代号",
					      	"mention_name":"资料名称"
					},"text":{
						    "id":obj.id==null?"":obj.id
						    ,"major_id":obj.major_id==null?"":obj.major_id
						    ,"major_name":obj.project_design_major_name==null?"":obj.project_design_major_name
						    ,"order_num":obj.order_num==null?"":obj.order_num
						    ,"mention_type":""
							,"mention_code":obj.mention_information_code==null?"":obj.mention_information_code
							,"mention_name":obj.mention_information_name==null?"":obj.mention_information_name
					},"input":{
						    "id":"hidden"
						    ,"major_id":"hidden"
						    ,"major_name":"text_readonly"
						    ,"order_num":"text"
						    ,"mention_type":"switch"
							,"mention_code":"hidden"
							,"mention_name":"text"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
		initSwitch(obj.mention_information_type);
  }
   
   var layopen = function(content){
		layer.open({
		  	title:'更新提入资料信息'
			,content: content
	    	,area: ['800px', '500px']
			,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(data.mention_code == ""){
					var obj = $("input[name='mention_code']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.mention_name == ""){
					var obj = $("input[name='mention_name']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.order_num == ""){
					var obj = $("input[name='order_num']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.major_id == ""){
					alert("请选择专业!")
					return false;
				}
				if(data.mention_type == "on"){
					data.mention_type = true;
				}else{
					data.mention_type = false;
				}
				$.post(base+"/dataCenter/businessdata/updateMentionsInfo",{
					id:data.id==null?"":data.id,
					major_id:data.major_id,
					mention_information_code:$.trim(data.mention_code),
					mention_information_name:$.trim(data.mention_name),
					mention_information_type:$.trim(data.mention_type),
					order_num:$.trim(data.order_num)
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addObj);
       $("#searchInfo").click(searchObj);
   });

   